package com.dataart.productshop.somepackageforspringstopfuckmybrain;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileDownloadController {
	
	@RequestMapping(value = "/download/{filename}", method = RequestMethod.GET)
	public String downloadFile(@PathVariable("filename") String filename, 
			HttpServletResponse response){

        InputStream is = null;
		try{
			System.out.println(filename);
		    is = new FileInputStream(new File(filename + ".txt"));
			IOUtils.copy(is, response.getWriter());
			response.setContentType("application/txt");
			response.flushBuffer();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
            IOUtils.closeQuietly(is);
        }

		return "home";
	}
}
